package com.NeuglsWorkStudio.UI.Buttons
{
	import com.NeuglsWorkStudio.Colors.HSL;
	import com.NeuglsWorkStudio.Colors.RGB;
	import com.NeuglsWorkStudio.Utility.Utility;
	
	import flash.display.BitmapData;
	import flash.display.CapsStyle;
	import flash.display.GradientType;
	import flash.display.LineScaleMode;
	import flash.display.Sprite;
	import flash.geom.Matrix;

	/**added some effect to TTipButton**/
	public class TTipButtonEx extends TTipButton
	{
		public static const bsNormal:int=0;
		public static const bsGradient:int=1;
		
		private var _FBaseColor:int;
		private var _FStyle:int=bsGradient;
		private var _FWidth:Number;
		private var _FHeight:Number;
		private var _FColorMatrix:Matrix;
		private var _FColorArray:Array;
		private var _FGradientColorArray:Array;
		private var _FGradientBack:Sprite;
		
		public function TTipButtonEx(Caption:String,Width:Number,Height:Number, DisplayCaption:Boolean=true,BaseColor:int=0x222222,Style:int=0)
		{
			super(Caption, DisplayCaption);
			_FBaseColor=BaseColor;
			_FWidth=Width;
			_FHeight=Height;
			_FStyle=Style;
			
			_FColorArray = [0,148,148,255];
			_FColorMatrix = new Matrix();
			_FColorMatrix.createGradientBox(_FWidth,_FHeight,Math.PI/2,0,0);
			_FGradientColorArray=BuildGradientColorArray(_FBaseColor);
			TipperYOffset=-Height/2-5;
			BuildGradientBack();
		}
		
		public function get BaseColor():int
		{
			return _FBaseColor;
		}

		public function set BaseColor(value:int):void
		{
			_FBaseColor = value;
			_FGradientColorArray=BuildGradientColorArray(_FBaseColor);
			BuildGradientBack();
		}


		override protected function DrawBackground(State:int):void{
			ProcessBack(State);
		}
		
		private function ProcessBack(state:int):void{
			switch(state){
				case stNormal:
					_FContainer.graphics.clear();
					break;
				case stMouseOver:
					DrawMouseOver();
					break;
				case stMouseDown:
					break;
				case stDisabled:
					break;
				default:
			}
		}
		private function BuildGradientBack():void{
			_FGradientBack=new Sprite();
			
			var bitmap:BitmapData=Utility.CreateGradientBitmapData(_FWidth,
				_FHeight,_FBaseColor,false);
			var lastX:Number=0;
			var lastY:Number=0;
			var lineSize:Number=1;
			var BorderColor:int=HSL.AddLightToColor(_FBaseColor,0.59);
			var DarkColor:int=HSL.AddLightToColor(_FBaseColor,-0.69);
			with(_FGradientBack.graphics){
				clear();
				lineStyle(lineSize,DarkColor,0.2,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
				beginBitmapFill(bitmap,null,false,false);
				drawRoundRect(lastX,lastY,_FWidth,_FHeight,10,10);
				endFill();
			}
		}
		private function DrawMouseOver():void{
			switch(_FStyle){
				case bsNormal:
					NormalMouseOver();
					break;
				case bsGradient:
					GradientMouseOver();
					break;
			}
		}
		
		protected function NormalMouseOver():void{
			var lineSize:Number=1;
			var BorderColor:int=HSL.AddLightToColor(_FBaseColor,0.59);
			var DarkColor:int=HSL.AddLightToColor(_FBaseColor,-0.69);
			var lastX:Number=-_FWidth/2;
			var lastY:Number=-_FHeight/2;
			var Round:Number=10;
			with(_FContainer.graphics){
				clear();
				lineStyle(lineSize,DarkColor,0.2,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
				drawRoundRect(lastX,lastY,_FWidth-lineSize*2,_FHeight-lineSize*2,Round,Round);
				lineStyle(lineSize,BorderColor,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
				beginGradientFill(GradientType.LINEAR,_FGradientColorArray,[1,1,1,1],_FColorArray,_FColorMatrix);
				drawRoundRect(lastX+lineSize,lastY+lineSize,_FWidth-4*lineSize,_FHeight-4*lineSize,Round,Round);
				endFill();
			}
		}
		
		protected function GradientMouseOver():void{
			_FContainer.graphics.clear();
			//if(_FContainer.contains(_FGradientBack)){
			//	_FContainer.removeChild(_FGradientBack);
			//}
			with(_FContainer.addChildAt(_FGradientBack,0)){
				x=-width/2;
				y=-height/2;
			}
		}
		
		protected function BuildGradientColorArray(BaseColor:int):Array{
			//return [0xF5F5F5, 0xE0E0E0, 0xCFCFCF, 0xC0C0C0];
			/**return [
				RGB.AddToRGBColor(BaseColor,0x35),
				RGB.AddToRGBColor(BaseColor,0x20),
				RGB.AddToRGBColor(BaseColor,0xF),
				BaseColor
			]**/
			return [
				HSL.AddSaturation(BaseColor,10,0,0.2),
				HSL.AddSaturation(BaseColor,5,0,0.2),
				HSL.AddSaturation(BaseColor,5,0,0.2),
				HSL.AddSaturation(BaseColor,4,0,0.2)
			]
/**			return [
				HSL.AddLightToColor(BaseColor,0.2),
				HSL.AddLightToColor(BaseColor,0.1),
				HSL.AddLightToColor(BaseColor,0.1),
				HSL.AddLightToColor(BaseColor,0.05)
			]*/
		}
	}
}